<template>
    <div class="top">

        <div class="choose">
                 <span>
                  描述：<input type="text" class="search_child" placeholder="如:QQ号/描述" v-model="searchValue"></span>
            <span>
                  时间：<input type="date" class="search_child" v-model="messagePage.dateTime"></span>
            <span  class="search_click" @click="search_">搜索</span>
        </div>
    </div>
    <div class="bottom">
        <table cellpadding="0" cellspacing="0" >
            <thead>
            <tr>
                <th>序号</th>
                <th>用户</th>
                <th>账号</th>
                <th>IP地址</th>
                <th>时间</th>
                <th>内容</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="(item,index) in fuzzyQueries"  v-if="fuzzyQueries.length>0">
                <td style="width: 60px">{{ total.current > 1 ? (index+1)+14*(total.current-1) : index+1}}</td>
                <td style="width: 60px" class="user_img">
                    <img :src="'https://q1.qlogo.cn/g?b=qq&nk='+item.url+'&s=640'" alt="">
                </td>
                <td style="width: 120px" v-html="highlight_(item.url)"></td>
                <td style="width: 100px" class="overflow" v-html="highlight_(item.ip)"></td>
                <td style="width:180px ">{{ item.time }}</td>
                <td class="overflow" v-html="highlight_(item.content)"></td>
                <td style="width: 150px">
                    <span class="hover button_style but_detail" @click="detail(item.content)" >详情</span>
                    <span class="hover button_style but_mistake" @click="edit_userInfo(item,index)">删除</span>
                </td>
            </tr>
            <tr v-else-if="fuzzyQueries.length===0 && load_" style="height: 50vh;background: transparent">
                <LoadTheAnimation ></LoadTheAnimation>
            </tr>
            <tr v-show="!load_">
                <td></td>
                <td></td>
                <td></td>
                <td><h2>未查询到任何数据</h2></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            </tbody>

        </table>

    </div>
    <div class="bottom_">
        <span style="margin-right:10px">共{{ total.page }}页, 共{{ total.sum }}条数据 当前第{{ total.current }}页</span>
        <span class="jian" @click="left_">
      <img src="../../../public/static/jian_r.png" alt="">
      </span>
        <input type="text" class="limit" v-model="total.current" @focusout="limit_">
        <span class="jian" @click="right_">
      <img src="../../../public/static/jian_l.png" alt="">
      </span>
        <select @change ="page_($event)" class="page_index">
            <option  v-for="(item,index) in 3" >{{(index+1)*10+4}}/条</option>
        </select>
    </div>
    <PromptWindow v-show="box_confirm" @confirm="confirm" :info="info" :index="index"></PromptWindow>
    <DetailsWindow @down="down" v-show="box" :info="info"></DetailsWindow>
</template>

<script>
import api from "@/api";
import DetailsWindow from "@/components/publicCommon/DetailsWindow.vue";
import PromptWindow from "@/components/publicCommon/PromptWindow.vue";
import LoadTheAnimation from "@/components/publicCommon/LoadTheAnimation.vue";

export default {
    name: "BlessingMessage",
    components: {LoadTheAnimation, PromptWindow, DetailsWindow},
    data() {
        return {
            select: false,
            searchValue: "",
            fuzzyQueries: [],
            messagePage: {
                index: 1,
                condition: null,
                types: null,
                page:14,
                dateTime:null,
            },
            messageDel: {
                index: "",
                condition: "",
            },
            total: {
                sum: '',
                page: '',
                size: '',
                current: '',
            },
            box:false,
            info:"",
            box_confirm:false,
            index:"",
            del_info:false,
            load_:true,
        }

    },
    mounted() {
        this.request_(this.messagePage);
    },
    watch: {
        'messagePage.page'(newValue,oldValue){
            this.messagePage.page = newValue;
            this.request_(this.messagePage);
        },
        'messagePage.dateTime'(newValue,oldValue){
            if(newValue == "") {
                this.messagePage.dateTime = null
                this.request_(this.messagePage);
            }
        },
        del_info(newValue,oldValue){
            if(newValue){
                api.delAdminLove(this.info.id).then((res) => {
                    if (res.data.code === 200) {
                        this.$store.commit("requestInfo", "删除留言成功");
                        this.$forceUpdate();
                    } else {
                        this.$store.commit("requestInfo", res.data.msg);
                    }
                    this.info = "";
                    this.del_info = false;
                })
            }
        },
        'total.current'(newVal, oldVal) {
            let reg = /^\+?[1-9]\d*$/;
            if(reg.test(newVal)){
                this.messagePage.index = newVal;
            }
            if(newVal > this.total.page){
                this.messagePage.index = 1;
            }
        },
        searchValue(newValue, oldValue) {
            if (newValue === "") {
                this.messagePage.condition = null;
                this.request_(this.messagePage);
            }else{
                this.messagePage.condition = newValue;
            }

        },
    },
    methods: {
        page_(event){
            this.messagePage.page = parseInt(event.target.value);
        },
        detail(item){
            this.info = item;
            this.box = true;
        },
        down(){
            this.info = "";
            this.box = false;
        },
        confirm(index,bool){
            this.box_confirm = false;
            if(bool){
                this.del_info  = true;
                this.fuzzyQueries.splice(index, 1);
            }
        },
        highlight_(text) {
            text = text + "";
            if (this.searchValue !== "")
                return text.replace(this.searchValue, '<span style="background: #f6f604;">' + this.searchValue + '</span>')
            else return text
        },
        edit_userInfo(info, index) {
            this.info = info;
            this.index = index;
            this.box_confirm = true;
        },
        search_() {
            this.request_(this.messagePage);
        },

        condition_(type) {
            this.messagePage.index = 1;
            this.messagePage.types = type;
            this.request_(this.messagePage);
        },
        limit_() {
            this.request_(this.messagePage);
        },
        request_(messagePage) {
            api.getAdminLove(messagePage).then((res) => {
                if(res.data.code === 200){
                    this.fuzzyQueries = res.data.data.records;
                    this.total.sum = res.data.data.total;
                    this.total.page = res.data.data.pages;
                    this.total.current = res.data.data.current;
                    this.total.size = res.data.data.size;
                    if(this.fuzzyQueries.length===0){
                        setTimeout(()=>{
                            this.load_ = false;
                        },1000)
                    }else {
                        this.load_ = true;
                    }
                }else {
                    setTimeout(()=>{
                        this.load_ = false;
                    },200)
                }
            })
        },
        left_() {
            this.messagePage.index -= 1;
            if (this.messagePage.index < 1) {
                this.messagePage.index = 1;
                this.$store.commit("requestInfo", "已是第一页");
                return;
            }
            this.request_(this.messagePage);
        },
        right_() {
            this.messagePage.index += 1;
            if (this.messagePage.index > this.total.page) {
                this.messagePage.index = this.total.page;
                this.$store.commit("requestInfo", "已是最后一页");
                return;
            }
            this.request_(this.messagePage);
        }
    }
}
</script>

<style scoped>


tr {
    max-width: 200px !important;
    text-align: center;
    height: 45px;
    background: #fff;
    margin-bottom: 5px;
    animation: tr_ .3s ease-in-out 0s 1 forwards;
}
@keyframes tr_ {
    0%{
        transform: scaleY(0%);
    }100%{
         transform: scaleY(100%);
     }
}
tr:nth-child(2n) {
    background: rgba(206, 202, 202, 0.5);
}

.search_child{
    height: 30px;
    padding:0 10px;
    outline:none;
}
.choose{
    margin-left: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.choose span{
    margin-right: 10px;
}
.search_click{
    display: inline-block;
    background: #057cf8;
    padding: 4px 7px;
    color: white;
    cursor: pointer;
}
.search_click:hover{
    opacity: .8;
}
.page_index{
    height: 26px;
    outline: none;
    padding: 0 7px;
    color: #4297ec;
    border: 1px solid #282828;
}
option{
    padding: 10px;
}
</style>